//
//  HEBindListViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/5/5.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HEBindListViewController.h"
#import "HEBindUserModel.h"
#import "HEHealthRecordViewController.h"

@interface HEBindListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;

@property (nonatomic, strong) NSMutableArray <HEBindUserModel *>*bindUsers;

@end

@implementation HEBindListViewController

- (NSMutableArray<HEBindUserModel *> *)bindUsers {
    if (!_bindUsers) {
        _bindUsers = @[].mutableCopy;
    }
    return _bindUsers;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
 
    self.title = @"我家老人";
    
    [self.view addSubview:self.tableView];
        
    [self setCustomTableView:self.tableView];
    [self showEmpty];
    [self getBindUserData];
}

- (GDTableView *)tableView {
    if (!_tableView) {
        _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight , KScreenWidth, KScreenHeight - ZXNavBarHeight) style:UITableViewStylePlain];
        _tableView.separatorColor = CSplitLineColor;
        _tableView.scrollsToTop = YES;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];

        _tableView.backgroundColor = CViewBgColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        __weak __typeof(&*self)weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            weakSelf.isFromHeadRefresh = YES;
            [weakSelf getBindUserData];
        }];
    }
    return _tableView;
}

- (void)getBindUserData {
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValue:[UserManager sharedUserManager].curUserInfo.id forKey:@"gid"];
    
    KWeakSelf
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KgetBindUser parameters:dict success:^(id  _Nonnull responseObject) {
        HEArrayResponse *response = [HEArrayResponse mj_objectWithKeyValues:responseObject];
        [self.bindUsers removeAllObjects];
        [weakSelf.bindUsers addObjectsFromArray:[HEBindUserModel mj_objectArrayWithKeyValuesArray:response.data]];
     
        [self.tableView.mj_header endRefreshing];

        if (self.bindUsers.count == 0) {
            [self showEmpty];
        }
        
        [self.tableView reloadData];
    } failure:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.bindUsers.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
        [cell.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(60);
            make.centerY.equalTo(cell.contentView);
            make.left.equalTo(cell.contentView).offset(15);
        }];
        
        cell.imageView.layer.masksToBounds = YES;
        cell.imageView.layer.cornerRadius = 30;
        
        [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.left.equalTo(cell.imageView.mas_right).offset(20);
            make.right.equalTo(cell.contentView).offset(-15);
        }];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    HEBindUserModel *info = _bindUsers[indexPath.row];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:info.avatar] placeholderImage: IMAGE_NAMED(@"defualt_icon")];
    cell.textLabel.text = info.realname;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HEBindUserModel *info = _bindUsers[indexPath.row];

    HEHealthRecordViewController *vc = [HEHealthRecordViewController new];
    vc.userModel = info;
    [self.navigationController pushViewController:vc animated:YES];

}

#pragma mark -
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        [self unbindUser:self.bindUsers[indexPath.row]];
    }];

    return @[deleteAction];
}

- (void)unbindUser:(HEBindUserModel *)model {
    NSMutableDictionary *dict = @{}.mutableCopy;
    [dict setValue:[NSNumber numberWithInteger:model.bind_id] forKey:@"id"];
    
    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KunbindRela parameters:dict success:^(id  _Nonnull responseObject) {
        HEResponse *response = [HEResponse mj_objectWithKeyValues: responseObject];
        if (response.success) {
            [Dialog toastBottom: @"删除成功"];
            if (self.backBlock) {
                self.backBlock();
            }
            [self.tableView.mj_header beginRefreshing];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

@end
 
